package com.rocky.ssm.controller;

import com.rocky.ssm.model.Score;
import com.rocky.ssm.service.IScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */

@Controller
@RequestMapping("/students")
public class ScoreController {

    @Autowired
    private IScore score;

    @RequestMapping("/scoreList")
    public String list(Model model){
        List<Score> list = score.getScoreList();
        model.addAttribute("list",list);
        return "score/list";
    }

    @RequestMapping("/{id}/scoreUpdate")
    public String update(@PathVariable int id, Model model){
       Score s= score.getScoreById(id);
       model.addAttribute("score",s);
       return "score/update";
    }

    @RequestMapping(value = "/doScoreUpdate",method = RequestMethod.POST)
    public String doUpdate(Score s){
        if(score.update(s)>0)
            return "redirect:/students/scoreList";
        else
            return "score/update";
    }

    @RequestMapping("/{id}/scoreDel")
    public String delete(@PathVariable int id){
        score.delete(id);
        return "redirect:/students/scoreList";
    }

    @RequestMapping("/skipScoreAdd")
    public String skipAdd(){
        return "score/add";
    }

    @RequestMapping(value = "/scoreAdd",method = RequestMethod.POST)
    public String add(Score s){
        if (score.save(s)>0)
            return "redirect:/students/scoreList";
        else
            return "score/add";

    }

}
